package com.meihua.baoleyi.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meihua.baoleyi.R;

/**
 * Created by amine on 16/7/12.
 */
public class ProductInfoPop extends PopupWindow {
    private ImageView text_back,iv_head;
    private TextView Balance,tv_balance,tv_affirm;
    private RelativeLayout rl_editsex, rl_Balance,rl_editsex_wx;
    private CheckBox check_editsex_balance;
    private CheckBox check_editsex;
    private CheckBox check_editsex_wx;
    private Context constant;
    private View mMenuView;

    public ProductInfoPop(Context context) {
        super(context);
        this.constant = context;
        mMenuView = LayoutInflater.from(context).inflate(R.layout.my_montebag_pop, null);
        tv_affirm = (TextView) mMenuView.findViewById(R.id.tv_affirm);
        Balance = (TextView) mMenuView.findViewById(R.id.Balance);
        tv_balance = (TextView) mMenuView.findViewById(R.id.tv_balance);
        rl_editsex = (RelativeLayout) mMenuView.findViewById(R.id.rl_editsex);//余额
        rl_Balance = (RelativeLayout) mMenuView.findViewById(R.id.rl_Balance);//支付宝
        rl_editsex_wx = (RelativeLayout) mMenuView.findViewById(R.id.rl_editsex_wx);//微信
        check_editsex_balance = (CheckBox) mMenuView.findViewById(R.id.check_editsex_balance);
        check_editsex = (CheckBox) mMenuView.findViewById(R.id.check_editsex);
        check_editsex_wx = (CheckBox) mMenuView.findViewById(R.id.check_editsex_wx);
        text_back = (ImageView) mMenuView.findViewById(R.id.text_back);
        iv_head = (ImageView) mMenuView.findViewById(R.id.iv_head);


        //设置按钮监听
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.take_photo_anim);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //实例化一个ColorDrawable颜色为半透明
/*        ColorDrawable dw = new ColorDrawable(FFFFFF);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);*/
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
        text_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
        //余额
        check_editsex_balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    check_editsex.setChecked(false);
                    check_editsex_wx.setChecked(false);
                    check_editsex_balance.setChecked(true);

            }
        });
        rl_Balance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    check_editsex.setChecked(false);
                    check_editsex_wx.setChecked(false);
                    check_editsex_balance.setChecked(true);

            }
        });
        //支付宝
        check_editsex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    check_editsex_balance.setChecked(false);
                    check_editsex_wx.setChecked(false);
                    check_editsex.setChecked(true);

            }
        });
        rl_editsex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    check_editsex_balance.setChecked(false);
                    check_editsex_wx.setChecked(false);
                    check_editsex.setChecked(true);

            }
        });
        //微信
        check_editsex_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    check_editsex.setChecked(false);
                    check_editsex_balance.setChecked(false);
                    check_editsex_wx.setChecked(true);

            }
        });
        rl_editsex_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    check_editsex.setChecked(false);
                    check_editsex_balance.setChecked(false);
                    check_editsex_wx.setChecked(true);

            }
        });

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) constant).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) constant).getWindow().setAttributes(lp);
    }

    private TextView tvBase = null;


    public void setPopData(String s, final PayCallBack payCallBack) {
        backgroundAlpha(0.5f);
        if (s.equals("余额充值")){
            rl_Balance.setVisibility(View.GONE);
        }else {
            rl_Balance.setVisibility(View.VISIBLE);
        }
        Balance.setText("当前可用余额为"+s);


      tv_affirm.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if (payCallBack != null&&check_editsex_balance.isChecked()) {
                  payCallBack.pay("余额");

              }else if (payCallBack != null&&check_editsex.isChecked()){
                  payCallBack.pay("支付宝");
              }else if (payCallBack != null&&check_editsex_wx.isChecked()){
                  payCallBack.pay("微信");
              }
          }
      });
    }

    public interface PayCallBack {
        void pay(String s);
    }
}
